package com.java1412.goods.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author java1412
 * @since 2024-04-25 10:07:22
 */
@RestController
@RequestMapping("/api/v1/sentinel")
public class SentinelTestController {

    // volatile：当total变量的值修改了，会立马同步到内核态中total变量上
    private volatile int total = 0;

    @Value("${server.port}")
    private String port;

    @RequestMapping(value = "/test-sentinel", produces = {"application/json;charset=utf-8"})
    public Object findByGoodsId() {
        System.out.println("端口号:" + port);
        return total++;
    }

    @RequestMapping(value = "/test-sentinel-exception", produces = {"application/json;charset=utf-8"})
    public Object testSentinelException() {
        int i = (int) (Math.random() * 100);
        if (i > 90) {
            throw new NullPointerException("随机错误");
        }
        return "成功";
    }
}
